﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.UI;
@using SimpleIdServer.IdServer.Website.Models;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.ScopeStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits FluxorComponent
@inject IState<UpdateScopeMapperState> updateScopeMapperState
@inject IDispatcher dispatcher

<RadzenTemplateForm Submit=@SubmitScopeMapper TItem="UserAttributeScopeMapperForm" Data=@scopeMapperFom>
    @if (!updateScopeMapperState.Value.IsUpdating && !string.IsNullOrWhiteSpace(updateScopeMapperState.Value.ErrorMessage))
    {
        <RadzenAlert AllowClose="false" AlertStyle="AlertStyle.Danger" ShowIcon="true" Variant="Variant.Flat" Shade="Shade.Lighter">@updateScopeMapperState.Value.ErrorMessage</RadzenAlert>
    }

    <UserScopeMapper UserScopeMapperForm="@scopeMapperFom" Scope="@Scope" IsEditModeEnabled="@IsEditModeEnabled"></UserScopeMapper>
    <!-- User attribute -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.UserAttribute</RadzenText>
        <RadzenTextBox Name="UserAttributeName" @bind-Value="@scopeMapperFom.UserAttributeName" Class="w-100"></RadzenTextBox>
        <RadzenRequiredValidator Component="UserAttributeName" Text="@Global.UserAttributeRequired"></RadzenRequiredValidator>
        <p class="text-muted">Name of stored attribute</p>
    </div>
    <!-- Multivalued  -->
    <div class="mt-1">
        <RadzenSwitch @bind-Value=@scopeMapperFom.Multivalued Name="Multivalued" TValue="bool" />
        <RadzenLabel Text="@Global.Multivalued" Component="Multivalued" Style="margin-left: 8px; vertical-align: middle;" />
    </div>
    <!-- Include the claims into the access token -->
    <div class="mt-1">
        <RadzenSwitch @bind-Value=@scopeMapperFom.IncludeInAccessToken Name="IncludeInAccessToken" TValue="bool" />
        <RadzenLabel Text="@Global.IncludeInAccessToken" Component="IncludeInAccessToken" Style="margin-left: 8px; vertical-align: middle;" />
    </div>
    <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(updateScopeMapperState.Value.IsUpdating ? Global.Saving : Global.Save)" Disabled="@(updateScopeMapperState.Value.IsUpdating)" />
</RadzenTemplateForm>
@code {
    [Parameter]
    public EventCallback<ScopeClaimMapper> ScopeMapperSaved { get; set; }
    [Parameter]
    public ScopeClaimMapper ClaimMapper { get; set; } = null!;
    [Parameter]
    public Scope Scope { get; set; } = null!;
    [Parameter]
    public bool IsEditModeEnabled { get; set; } = false;

    record UserAttributeScopeMapperForm : BaseUserScopeMapperForm
    {
        public string UserAttributeName { get; set; } = null!;
        public bool Multivalued { get; set; } = false;
    }

    UserAttributeScopeMapperForm scopeMapperFom = new UserAttributeScopeMapperForm();

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (ClaimMapper == null) return;
        scopeMapperFom.Update(ClaimMapper);
        scopeMapperFom.UserAttributeName = ClaimMapper.SourceUserAttribute;
        scopeMapperFom.Multivalued = ClaimMapper.IsMultiValued;
    }

    async void SubmitScopeMapper()
    {
        var result = scopeMapperFom.Build();
        result.MapperType = MappingRuleTypes.USERATTRIBUTE;
        result.SourceUserAttribute = scopeMapperFom.UserAttributeName;
        result.IsMultiValued = scopeMapperFom.Multivalued;
        await ScopeMapperSaved.InvokeAsync(result);
    }
}